package com.example.demo.controller;

import com.aliyun.oss.OSS;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.example.demo.configuration.AliyunOssConfig;
import com.example.demo.dto.*;
import com.example.demo.controller.bean.Result;
import com.example.demo.mapper.PointsMallManageMapper;
import com.example.demo.mapper.entity.*;
import com.example.demo.service.PointsMallManageService;
import com.example.demo.utils.PageBo;
import com.example.demo.utils.PageVo;
import com.example.demo.utils.TreeBo;
import com.fasterxml.jackson.databind.ObjectMapper;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.UUID;

@RequestMapping("pointsMallManage")
@Api(value = "商城管理接口", tags = {"商城管理接口"})
@RestController
@Slf4j
public class PointsMallManageController {
    @Autowired
    private PointsMallManageService service;
    @Autowired
    private PointsMallManageMapper mapper;

    private OSS ossClient;

    /*商品管理*/
    @ResponseBody
    @ApiOperation(value = "获取商品列表接口")
    @RequestMapping(value = "getCommodityList", method = {RequestMethod.POST})
    public Result<PageVo<Commodity>> getCommodityList(@RequestBody PageBo pageBo) {
        PageVo<Commodity> list = service.getCommodityList(pageBo);
        return Result.ok(list);
    }

    @ResponseBody
    @ApiOperation("商品圖片显示接口")
    @RequestMapping(value = "findCommodityPic/{commodityId}")
    public Result<String> findCommodityPic(@PathVariable("commodityId")int commodityId) {
        Result<String> message = new Result<>();
        if (commodityId != 0) {
            Commodity result = service.getById(commodityId);

            String imageUrl = result.getImageUrl();
            if(imageUrl != null){
                message.setData("https://seedling-classmate.oss-cn-beijing.aliyuncs.com/" + imageUrl);
                return message;
            }
            else{
                return null;
            }
        }
        return null;
    }

    @ResponseBody
    @ApiOperation(value = "搜索商品接口")
    @RequestMapping(value = "searchCommodityList", method = {RequestMethod.POST})
    public Result<PageVo<Commodity>> getCommoditySearch(@RequestBody CommodityRequestDTO request) {
        PageBo pageBo = new PageBo();
        pageBo.setPageNum(request.getPageNum());
        pageBo.setPageSize(request.getPageSize());

        PageVo<Commodity> logs = service.searchCommodityList(request,pageBo);
        return Result.ok(logs);
    }


    //更新商品
    @ResponseBody
    @ApiOperation("修改商品接口")
    @RequestMapping(value = "updateCommodityList", method = {RequestMethod.POST})
    public Result<String> updateAttributeList(@RequestBody CommodityRequestDTO request) {
        return service.updateCommodityList(request);
    }

    @ApiOperation(value = "商品圖片上传")
    @PostMapping("/uploadCommodity")
    public Result uploadCommodity(@RequestParam("commodityId") int commodityId, @RequestParam("file") MultipartFile file) throws IOException {

        log.info(file.toString());
        // 获取当前会话账号id
        System.out.println("当前登录账号id为：" + commodityId);

        String originalFileName = file.getOriginalFilename();//从上传的文件中获取原始文件名
        int i = originalFileName.lastIndexOf(".");
        String png = originalFileName.substring(i);//获取文件扩展名

        //存储空间的名字
        String bucketName = "seedling-classmate";

        //创建存储对象
        ossClient = AliyunOssConfig.ossClient();

        //设置文件名并上传到OSS
        String fileName = "commodity/" + UUID.randomUUID().toString().substring(0,24)+png;
        ossClient.putObject(bucketName, fileName, file.getInputStream());   //之后在数据库中保存fileName

        Commodity currentCommdity = service.selectCommodityById(commodityId);
        currentCommdity.setImageUrl(fileName);
        service.placeCommodity(currentCommdity);
        ossClient.shutdown();

        String result="http://seedling-classmate.oss-cn-beijing.aliyuncs.com/"+fileName;
        return Result.ok("上传成功",result);

    }

    @ResponseBody
    @ApiOperation("删除屬性接口")
    @RequestMapping(value = "deleteCommodityList", method = {RequestMethod.POST})
    public Result<String> deleteCommodityList(@RequestBody Commodity commodity) {
        return service.deleteCommodityList(commodity);
    }

    @ResponseBody
    @ApiOperation("批量删除接口")
    @RequestMapping(value ="batchDeleteCommodityList", method = {RequestMethod.POST})
    public Result<String> batchDeleteCommodityList(@RequestBody List<Commodity> commodityList) {
        service.batchDeleteCommodityList(commodityList);
        return Result.ok("删除成功");
    }

    /*订单管理*/
    @ResponseBody
    @ApiOperation(value = "获取待处理订单列表接口")
    @RequestMapping(value = "getPendingOrderList", method = {RequestMethod.POST})
    public Result<PageVo<Order>> getPendingOrderList(@RequestBody PageBo pageBo) {
        PageVo<Order> list = service.getPendingOrderList(pageBo);
        return Result.ok(list);
    }

    @ResponseBody
    @ApiOperation(value = "获取已发货订单列表接口")
    @RequestMapping(value = "getShippedOrderList", method = {RequestMethod.POST})
    public Result<PageVo<Order>> getShipedOrderList(@RequestBody PageBo pageBo) {
        PageVo<Order> list = service.getShipedOrderList(pageBo);
        return Result.ok(list);
    }

    @ResponseBody
    @ApiOperation(value = "获取已完成订单列表接口")
    @RequestMapping(value = "getCompletedOrderList", method = {RequestMethod.POST})
    public Result<PageVo<Order>> getCompletedOrderList(@RequestBody PageBo pageBo) {
        PageVo<Order> list = service.getCompletedOrderList(pageBo);
        return Result.ok(list);
    }

    @ResponseBody
    @ApiOperation(value = "获取已取消订单列表接口")
    @RequestMapping(value = "getCanceledOrderList", method = {RequestMethod.POST})
    public Result<PageVo<Order>> getCanceledOrderList(@RequestBody PageBo pageBo) {
        PageVo<Order> list = service.getCanceledOrderList(pageBo);
        return Result.ok(list);
    }

    @ResponseBody
    @ApiOperation(value = "搜索订单接口")
    @RequestMapping(value = "searchOrderList", method = {RequestMethod.POST})
    public Result<PageVo<Order>> getOrderSearch(@RequestBody OrderRequestDTO request) {
        PageBo pageBo = new PageBo();
        pageBo.setPageNum(request.getPageNum());
        pageBo.setPageSize(request.getPageSize());

        PageVo<Order> logs = service.searchOrderList(request,pageBo);
        return Result.ok(logs);
    }

    @ResponseBody
    @ApiOperation("修改商品列表接口")
    @RequestMapping(value = "updateOrderList", method = {RequestMethod.POST})
    public Result<String> updatePointsList(@RequestBody OrderRequestDTO request) {
        return service.updateOrderList(request);
    }

    /*积分管理*/
    @ResponseBody
    @ApiOperation(value = "获取积分列表接口")
    @RequestMapping(value = "getPointsList", method = {RequestMethod.POST})
    public Result<PageVo<StudentPoints>> getStudentPointsList(@RequestBody PageBo pageBo) {
        PageVo<StudentPoints> list = service.getStudentPointsList(pageBo);
        return Result.ok(list);
    }

    @ResponseBody
    @ApiOperation(value = "搜索积分列表接口")
    @RequestMapping(value = "searchPointsList", method = {RequestMethod.POST})
    public Result<PageVo<StudentPoints>> getStudentPointsSearch(@RequestBody StudentPointsRequestDTO request) {
        PageBo pageBo = new PageBo();
        pageBo.setPageNum(request.getPageNum());
        pageBo.setPageSize(request.getPageSize());

        PageVo<StudentPoints> logs = service.searchStudentPointsList(request,pageBo);
        return Result.ok(logs);
    }

    @ResponseBody
    @ApiOperation("修改积分列表接口")
    @RequestMapping(value = "updatePointsList", method = {RequestMethod.POST})
    public Result<String> updatePointsList(@RequestBody StudentPointsRequestDTO request) {
        return service.updatePointsList(request);
    }

    /*兑换管理*/
    @ResponseBody
    @ApiOperation(value = "获取兑换列表接口")
    @RequestMapping(value = "getExchangeRecordList", method = {RequestMethod.POST})
    public Result<PageVo<ExchangeRecord>> getExchangeRecordList(@RequestBody PageBo pageBo) {
        PageVo<ExchangeRecord> list = service.getExchangeRecordList(pageBo);
        return Result.ok(list);
    }

    @ResponseBody
    @ApiOperation(value = "搜索兑换列表接口")
    @RequestMapping(value = "searchExchangeRecordList", method = {RequestMethod.POST})
    public Result<PageVo<ExchangeRecord>>searchExchangeRecordList(@RequestBody ExchangeRecordRequestDTO request) {
        System.out.println("你好"+request);
        PageBo pageBo = new PageBo();
        pageBo.setPageNum(request.getPageNum());
        pageBo.setPageSize(request.getPageSize());

        PageVo<ExchangeRecord> logs = service.searchExchangeRecordList(request,pageBo);
        return Result.ok(logs);
    }

    @ResponseBody
    @ApiOperation("訂單詳情显示接口")
    @RequestMapping(value = "getOrderDetails/{orderId}")
    public Result<List<OrderView>> getOrderDataList(@PathVariable("orderId")int orderId){
        System.out.println("調用訂單詳情显示接口");
        List<OrderView> list = service.getOrderDataList(orderId);
        return Result.ok(list);
    }

    @GetMapping("getTimeLine/{orderId}")
    @ApiOperation("订单追踪显示接口")
    @ResponseBody
    public Result<List<OrderTimeLineDTO>> getTimeLineList(@PathVariable("orderId")int orderId){
        List<OrderTimeLineDTO> list = service.getTimeLineList(orderId);
        return Result.ok(list);
    }

    @ResponseBody
    @ApiOperation("取消訂單接口")
    @RequestMapping(value = "cancelOrder", method = {RequestMethod.POST})
    public Result<String> cancelOrder(@RequestBody Order request) {
        return service.cancelOrder(request);
    }

    @ResponseBody
    @ApiOperation("刪除訂單接口")
    @RequestMapping(value = "deleteOrder/{orderId}", method = {RequestMethod.POST})
    public Result<String> deleteOrder(@PathVariable("orderId")int orderId) {
        return service.deleteOrder(orderId);
    }

    @ResponseBody
    @ApiOperation("訂單發貨接口")
    @RequestMapping(value = "shipOrder", method = {RequestMethod.POST})
    public Result<String> shipOrder(@RequestBody Order request) {
        return service.shipOrder(request);
    }

    @ApiOperation(value = "新增優惠券")
    @PostMapping("/addCommodity")
    public Result<String> addCoupons(@RequestParam("file") MultipartFile file, @RequestParam("addData") String addData) throws IOException {
        log.info(file.toString());

        Commodity curCommodity = new ObjectMapper().readValue(addData, Commodity.class);

        System.out.println("CouponsRequestDTO返回值"+curCommodity);

        log.info(file.toString());

        String originalFileName = file.getOriginalFilename();//从上传的文件中获取原始文件名
        int i = originalFileName.lastIndexOf(".");
        String png = originalFileName.substring(i);//获取文件扩展名

        //存储空间的名字
        String bucketName = "seedling-classmate";

        //创建存储对象
        ossClient = AliyunOssConfig.ossClient();

        //设置文件名并上传到OSS
        String fileName = "commodity/" + UUID.randomUUID().toString().substring(0,24)+png;
        ossClient.putObject(bucketName, fileName, file.getInputStream());   //之后在数据库中保存fileName

        ossClient.shutdown();
        return service.addCommodity(curCommodity,fileName);

    }

    @ResponseBody
    @ApiOperation("修改收貨人信息接口")
    @RequestMapping(value = "updateReceiverInfo", method = {RequestMethod.POST})
    public Result<String> updateReceiverInfo(@RequestBody Order request) {
        return service.updateReceiverInfo(request);
    }
}
